package com.kgc.ssm.controller;

import com.kgc.ssm.entity.*;
import com.kgc.ssm.entity.dto.Result;
import com.kgc.ssm.service.PurchaseService;
import com.kgc.ssm.service.StockService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/put")
public class PurchaseController {

    @Resource
    private PurchaseService purchaseService;

    /**上面这一部分是关于 编制询价书的**/
    //查询所有的数据
    @GetMapping("/getall")
    public Result<Object> getAll(){
        System.out.println("调用了put里面的getAll方法");
        List<Stock> all = purchaseService.findAll();
        System.out.println(all);
        return new Result<>("查询成功",200,all);
    }

    //测试使用表连接查询所有数据
    @GetMapping("/getall2")
    public Result<Object> getAll2(){
        System.out.println("调用了put里面的getAll2方法");
        List<Stock> all = purchaseService.findAll2();
        System.out.println(all);
        return new Result<>("查询成功",200,all);
    }

    //根据id多表查询所有数据
    @GetMapping("/getbyid")
    public Result<Object> getById(Integer id){
        System.out.println("调用了put里面的getById方法");
        Map<String, Object> byId = purchaseService.findById(id);
        System.out.println(byId);
        return new Result<>("查询成功",200,byId);
    }

    //根据id更新数据
    @PostMapping("/update")
    public Result<Object> update(@RequestBody Map<String,Object> param){
        System.out.println("调用了put里面的update方法");
        System.out.println(param);
        int i = purchaseService.updateAll(param);
        String msg = i>0?"更新成功":"更新失败";
        int code = i>0?200:500;
        System.out.println(msg);
        return new Result<>(msg,code,null);
    }

    /**从这开始 是询价书列表的后端方法**/

    //两表查询 QUOTE表和ENQUIRE表的联合查询 查询出来返回列表数据
    @GetMapping("/getAllList")
    public Result<Object> getAllList(){
        List<Map<String, Object>> allList = purchaseService.getAllList();
        System.out.println(allList);
        return new Result<>("查询成功",200,allList);
    }

    //根据前端传过来的id删除对应的数据 两表连接进行删除 会删除两表对应的数据
    @GetMapping("/deleteQE")
    public Result<Object> deleteQE(Integer id){
        int i = purchaseService.DeleteById(id);
        String msg = i>0?"删除成功!":"删除失败!";
        int code = i>0?200:500;
        return new Result<>(msg,code,null);
    }

    //表连接查询 查询QUOTE和ENQUIRE两表的 所有数据
    @GetMapping("/selectAllQE")
    public Result<Object> selectAllQE(Integer id){
        System.out.println("调用了selectAllQE方法");
        System.out.println("id为:"+id);
        List<Map<String, Object>> maps = purchaseService.selectAllQE(id);
        return new Result<>("查询成功!",200,maps);
    }

    @GetMapping("/selectQuoted")
    public Result<Object> selectQuote(Integer id){
        System.out.println("调用了selectQuote方法");
        List<Map<String, Object>> maps = purchaseService.selectQuoted(id);
        return new Result<>("查询成功!",200,maps);
    }

    @GetMapping("/findAllQuote")
    public Result<Object> findAllQuote(Integer id){
        List<Map<String,Object>> allQuote = purchaseService.findAllQuote(id);
        return new Result<>("查询成功!",200,allQuote);
    }

    @GetMapping("/selectAllQD")
    public Result<Object> selectAllQD(Integer id){
        List<Map<String, Object>> allQD = purchaseService.selectAllQD(id);
        return new Result<>("查询成功!",200,allQD);
    }

    @GetMapping("/findAllQ")
    public Result<Object> findAllQE(Integer id){
        System.out.println("调用了findAllQE方法");
        List<Map<String, Object>> allQE = purchaseService.findAllQE(id);
        return new Result<>("查询成功!",200,allQE);
    }

    @GetMapping("selectByIdOP")
    public Result<Object> selectByIdOP(Integer id){
        System.out.println("调用了selectByIdOP方法");
        List<Map<String, Object>> byIdOP = purchaseService.selectByIdOP(id);
        return new Result<>("查询成功!",200,byIdOP);
    }

    @GetMapping("/selectByIdE")
    public Result<Object> selectByIdE(Integer id){
        System.out.println("调用了selectByIdE方法");
        List<Enquire> byIdE = purchaseService.selectByIdEnquire(id);
        return new Result<>("查询成功!",200,byIdE);
    }

    @PostMapping("/updateEnquire")
    public Result<Object> updateEnquire(@RequestBody Map<String, Object> param){
        System.out.println("调用了updateEnquire方法");
        System.out.println(param);
        int i = purchaseService.updateEnquire(param);
        String msg = i>0?"更新成功":"更新失败";
        int code = i>0?200:500;
        return new Result<>(msg,code,null);
    }

    @PostMapping("/updateOrder")
    public Result<Object> updateOrder(@RequestBody List<Map<String, Object>> params){
        System.out.println("调用了updateOrder方法");
        int i = 0;
        for(Map<String, Object> map:params){
            i+= purchaseService.updateOrder(map);
        }
        System.out.println("更新的行数为:"+i);
        String msg = i>0?"更新成功":"更新失败";
        int code = i>0?200:500;
        return new Result<>(msg,code,null);
    }

    @GetMapping("/selectAllContract")
    public Result<Object> selectAllContract(){
        List<Contract> allContract = purchaseService.selectAllContract();
        return new Result<>("查询成功!",200,allContract);
    }

    @GetMapping("/selectAllAppEnquire")
    public Result<Object> selectAllContractApply(){
        List<Map<String,Object>> allContractApply = purchaseService.selectAllAppEnquire();
        return new Result<>("查询成功!",200,allContractApply);
    }

    @GetMapping("/selectByEnquireName")
    public Result<Object> selectByEnquireName(String enquireName){
        Enquire byEnquireName = purchaseService.selectByName(enquireName);
        return new Result<>("查询成功!",200,byEnquireName);
    }

    @GetMapping("/selectByContId")
    public Result<Object> selectByContId(Integer id){
        List<ContractDetail> byContId = purchaseService.selectByContIdList(id);
        return new Result<>("查询成功!",200,byContId);
    }

}
